using System;
using System.Data;
using System.Data.SqlClient;

namespace FileServerDocumenter.Library
{
    class DataCache
    {
        //Shared Interface
        private static DataCache _DataCache;

        public static DataCache GetInstance()
        {
            try
            {
                if (_DataCache == null)
                { 
                    _DataCache = new DataCache();
                }
            }
            finally{}

            return _DataCache;
        }

        private DataCache()
        { 
        
        }

        public void CacheData(DataTable SourceTable)
        { 
            const string TARGET_TABLE = "dbo.FileInfo";

            string connectionString = getConnectionString();

            using (SqlConnection destinationCN = new SqlConnection(connectionString))
            {
                destinationCN.Open();

                //delete from the table
                SqlCommand cmd = new SqlCommand("TRUNCATE TABLE " + TARGET_TABLE);
                cmd.Connection = destinationCN;

                cmd.ExecuteNonQuery();

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationCN))
                {
                    bulkCopy.DestinationTableName = TARGET_TABLE;

                    try
                    {
                        foreach (DataRow dr in SourceTable.Rows)
                        {
                            
                        }
                        //write the data in
                        bulkCopy.WriteToServer(SourceTable);
                    }
                    catch( Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            }
        }

        private string getConnectionString()
        {
            return "Server=.\\SQLExpress;Database=ITSupportDesk";
        }
    }
}
